package com.adon.ar.test;

import com.she.olympic.R;
import com.she.olympic.R.raw;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

public class SoundSystem 
{
	private static SoundSystem ss = null;
	
	final static int SOUND_capture = 0;
	final static int SOUND_un_capture = 1;
//	final static int SOUND_SUPER = 2;
//	final static int SOUND_BOOM = 3;
//	final static int SOUND_HIT = 4;
//	final static int MUSIC_TITLE = 5;
//	final static int MUSIC_GAME = 6;
//	final static int MUSIC_TUTORIAL = 7;
	
	private SoundPool sp = null;
	private final int SOUND_COUNT = 5;
	private int[] nSound = new int[SOUND_COUNT];
	private MediaPlayer music_title = null;
	private MediaPlayer music_game = null;
	private MediaPlayer music_tutorial = null;

	private boolean isOn = true;
	
	public static SoundSystem GetInstance(Activity a)
	{
		if(ss == null)
		{
			ss = new SoundSystem(a);
		}
		
		return ss;
	}
	
	private SoundSystem(Activity a)
	{
//		music_title = MediaPlayer.create(a, R.raw.snd_title);
//    	music_title.setLooping(true);
//    	music_game = MediaPlayer.create(a, R.raw.snd_game);
//    	music_game.setLooping(true);
//    	music_tutorial = MediaPlayer.create(a, R.raw.snd_tutorial);
//    	music_tutorial.setLooping(true);
    	
    	sp = new SoundPool(SOUND_COUNT, AudioManager.STREAM_MUSIC, 0);
    	nSound[0] = sp.load(a, R.raw.capture, 1);
    	nSound[1] = sp.load(a, R.raw.un_capture, 1);
//    	nSound[2] = sp.load(a, R.raw.sn_super, 1);
//    	nSound[3] = sp.load(a, R.raw.sn_boom, 1);
//    	nSound[4] = sp.load(a, R.raw.sn_ef_hit2, 1);
	}
	
	public void playMusic(int nType)
	{
		if(!isOn) return;
		
		switch(nType)
		{
//		case MUSIC_TITLE:
//			music_title.seekTo(0);
//    		music_title.setVolume(0.8F, 0.8F);
//    		music_title.start();
//    		break;
//		case MUSIC_GAME:
//			music_game.seekTo(0);
//    		music_game.setVolume(0.2F, 0.2F);
//    		music_game.start();
//    		break;
//		case MUSIC_TUTORIAL:
//			music_tutorial.seekTo(0);
//    		music_tutorial.setVolume(0.9F, 0.9F);
//    		music_tutorial.start();
//    		break;
		}
	}
	
	public void stopMusic()
	{
		if(music_title.isPlaying()) music_title.pause();
		if(music_game.isPlaying()) music_game.pause();
		if(music_tutorial.isPlaying()) music_tutorial.pause();
	}
	
	public void playSound(int nType)
	{
		if(!isOn) return;
		
		switch(nType)
		{
		case SOUND_capture: sp.play(nSound[0], 0.5F, 0.5F, 0, 0, 1.0F); break;
		case SOUND_un_capture: sp.play(nSound[1], 0.5F, 0.5F, 0, 0, 1.0F); break;
//		case SOUND_SUPER: sp.play(nSound[2], 1.0F, 1.0F, 0, 0, 1.0F); break;
//		case SOUND_BOOM: sp.play(nSound[3], 1.0F, 1.0F, 0, 0, 1.0F); break;
//		case SOUND_HIT: sp.play(nSound[4], 1.0F, 1.0F, 0, 0, 1.0F); break;
		}
	}
	
	public void turnOn()
	{
		isOn = true;
	}
	
	public void turnOff()
	{
		isOn = false;
		stopMusic();
	}
	
	public static void release()
	{
		ss.music_title.stop();
		ss.music_title.release();
		ss.music_title = null;
		
		ss.music_game.stop();
		ss.music_game.release();
		ss.music_game = null;
		
		ss.music_tutorial.stop();
		ss.music_tutorial.release();
		ss.music_tutorial = null;
		
		ss.sp.release();
		ss.sp = null;
		
		ss = null;
	}
}
